/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaPrincipal.java
 *
 * Created on 31-may-2012, 10:25:25
 */
package edu.eam.gruposenior.gestorLEcciones.vista;

import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.TwilightSkin;

import edu.eam.gruposenior.gestorLEcciones.controladores.CategoriaController;
import edu.eam.gruposenior.gestorLEcciones.entidades.CategoriaSubcategoria;
import edu.eam.gruposenior.gestorLEcciones.entidades.LeccionAprendida;
import edu.eam.gruposenior.gestorLEcciones.vista.common.ButtonTabComponent;
import edu.eam.gruposenior.gestorLEcciones.vista.common.CategoriaTreeNode;
import edu.eam.gruposenior.gestorLEcciones.vista.common.LeccionAprendidaTreeNode;

/**
 *
 * @author Claudia
 */
public class VentanaPrincipal extends javax.swing.JFrame  {

    /** Creates new form VentanaPrincipal */
    private CategoriaController control;

    public VentanaPrincipal() {
        initComponents();
        //arbolLecciones.setComponentPopupMenu(jPopupMenu1);

        DefaultTreeCellRenderer render = (DefaultTreeCellRenderer) arbolLecciones.getCellRenderer();
        render.setLeafIcon(new ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Notepad.png")));
        render.setOpenIcon(new ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Address Book.png")));
        render.setClosedIcon(new ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Address-Book.png")));
        
        control = new CategoriaController();
        actualizarArbol();


    }

    /**
     * 
     * @param rol 
     */
    public void confGUI(String rol){
        
        if(rol.equals("Administrador")){
            mnuAdmon.setVisible(true);
        }else{
            mnuAdmon.setVisible(false);
        }
        
        
        
        
    }
    
    
    
    public void actualizarArbol() {
        CategoriaSubcategoria raiz = control.cargarRaiz();
        CategoriaTreeNode nodoRaiz = new CategoriaTreeNode(raiz, null);
        DefaultTreeModel dtm = (DefaultTreeModel) arbolLecciones.getModel();
        dtm.setRoot(nodoRaiz);
    }

    private void initTabComponent() {
        for (int i = 0; i < jTabbedPane1.getTabCount(); i++) {

            jTabbedPane1.setTabComponentAt(i,
                    new ButtonTabComponent(jTabbedPane1, new ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Notepad.png"))));

            // jTabbedPane1.setTabComponentAt(i,new JButton("algo"));
        }
    }

    public void agregarLeccion(LeccionAprendida leccion, CategoriaSubcategoria cat) {
        String nombre = null;
        if (leccion == null) {
            nombre = "nueva leccion";
        } else {
            nombre = leccion.getNombre();
        }
        jTabbedPane1.addTab(nombre, new ImageIcon(getClass().
                getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Notepad.png")), 
                new PanelLeccionAprendida(leccion, cat,this));
        initTabComponent();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        popMenuItemCrearLeccion = new javax.swing.JMenuItem();
        popMenuItemCrearCategoria = new javax.swing.JMenuItem();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        arbolLecciones = new javax.swing.JTree();
        jPanel2 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jToolBar1 = new javax.swing.JToolBar();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnuAdmon = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        mnuItemProyecto = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();

        popMenuItemCrearLeccion.setText("Crear Leccion");
        popMenuItemCrearLeccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                popMenuItemCrearLeccionActionPerformed(evt);
            }
        });
        jPopupMenu1.add(popMenuItemCrearLeccion);

        popMenuItemCrearCategoria.setText("Crear categoria");
        popMenuItemCrearCategoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                popMenuItemCrearCategoriaActionPerformed(evt);
            }
        });
        jPopupMenu1.add(popMenuItemCrearCategoria);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Lecciones Aprendidas");

        arbolLecciones.setRootVisible(false);
        arbolLecciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                arbolLeccionesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(arbolLecciones);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(jPanel2);

        jToolBar1.setRollover(true);

        mnuAdmon.setText("Administrador");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/WLM.png"))); // NOI18N
        jMenuItem1.setText("Usuario");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        mnuAdmon.add(jMenuItem1);

        mnuItemProyecto.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        mnuItemProyecto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Winrar.png"))); // NOI18N
        mnuItemProyecto.setText("Proyectos");
        mnuItemProyecto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItemProyectoActionPerformed(evt);
            }
        });
        mnuAdmon.add(mnuItemProyecto);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Java.png"))); // NOI18N
        jMenuItem3.setText("Tecnologias");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        mnuAdmon.add(jMenuItem3);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Desktop.png"))); // NOI18N
        jMenuItem2.setText("Registrar Usuario-Proyecto");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        mnuAdmon.add(jMenuItem2);

        jMenuBar1.add(mnuAdmon);

        jMenu2.setText("Usuario");

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Edit Users.png"))); // NOI18N
        jMenuItem6.setText("Actualizar Informacion");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem6);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Base de Conocimiento");

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Search.png"))); // NOI18N
        jMenuItem4.setText("Buscar En Lecciones");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE)
            .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private CategoriaSubcategoria categoriaSeleccionada;
    private void arbolLeccionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_arbolLeccionesMouseClicked
        // TODO add your handling code here:
        TreePath path = arbolLecciones.getSelectionPath();


        if (path != null) {
            TreeNode node = (TreeNode) path.getLastPathComponent();
            if (evt.getClickCount() == 2 && evt.getButton() == MouseEvent.BUTTON1) {

                if (node instanceof LeccionAprendidaTreeNode) {
                    LeccionAprendidaTreeNode leccionAprendidaTreeNode = (LeccionAprendidaTreeNode) node;

                    LeccionAprendida leccion = leccionAprendidaTreeNode.getLeccion();
                    agregarLeccion(leccion, null);
                }
            }

            if (evt.getButton() == MouseEvent.BUTTON3) {
                if (node instanceof CategoriaTreeNode) {
                    CategoriaTreeNode categoriaTreeNode = (CategoriaTreeNode) node;
                    categoriaSeleccionada = categoriaTreeNode.getCategoria();
                    popMenuItemCrearCategoria.setText("Crear Categoria a " + categoriaTreeNode.toString());
                    popMenuItemCrearLeccion.setText("Crear Leccion a " + categoriaTreeNode.toString());
                    jPopupMenu1.show(arbolLecciones, evt.getX(), evt.getY());
                }
            }
        }


    }//GEN-LAST:event_arbolLeccionesMouseClicked

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:

        new VentanaUsuarios().setVisible(true);

    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void popMenuItemCrearLeccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_popMenuItemCrearLeccionActionPerformed
        // TODO add your handling code here:
        agregarLeccion(null, categoriaSeleccionada);
    }//GEN-LAST:event_popMenuItemCrearLeccionActionPerformed

    private void popMenuItemCrearCategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_popMenuItemCrearCategoriaActionPerformed
        // TODO add your handling code here:
        String nombre=JOptionPane.showInputDialog( "Nombre de la nueva Categoria");
       
        control.crearCategoria(nombre, categoriaSeleccionada);
        actualizarArbol();
    }//GEN-LAST:event_popMenuItemCrearCategoriaActionPerformed

    private void mnuItemProyectoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuItemProyectoActionPerformed
        // TODO add your handling code here:new 
        new VentanaProyecto().setVisible(true);
    }//GEN-LAST:event_mnuItemProyectoActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
      // TODO add your handling code here:
        new VentanaTecnologia().setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        new VentanaAgregarUsuarioProyecto().setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        // TODO add your handling code here:
        new VentanaActualizarUsuario().setVisible(true);
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
        new  VentanaBuscarLecciones(this).setVisible(true);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                SubstanceLookAndFeel.setSkin(new TwilightSkin());

               
                new VentanaPrincipal().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree arbolLecciones;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenu mnuAdmon;
    private javax.swing.JMenuItem mnuItemProyecto;
    private javax.swing.JMenuItem popMenuItemCrearCategoria;
    private javax.swing.JMenuItem popMenuItemCrearLeccion;
    // End of variables declaration//GEN-END:variables
}
